// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import mainGame from "./mainGame";
import compare from "./compare";

//star存放星星的位置
const {ccclass, property} = cc._decorator;
@ccclass
export default class star extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    @property
    starSpace:number = 0;

    @property(cc.Prefab)
    starPreFab:cc.Prefab = null;


    @property({
        type: cc.Integer
    })
    score:number = 0;    
    
    @property(
        cc.String
    )
        displayName:string = "Score (player)";

    @property(
        cc.String
    )
        tooltip:string = "The score of player";    

    andMonsterInstance() {//gameScene  compare mainGame
        //获得距离
        // var monsterPosition = this.getComponent('star').gameScene.getPosition();
        // var starPosition:any = this.node.getPosition()
        /* // let starPosition:any = cc.find('Canvas/PurpleMonster').getPosition(); */
        //sub 向量坐标进行相减: sub(vector: Vec3, out?: Vec3): Vec3
        //mag 返回该向量的长度
        var starPosition:any = this.node.getPosition();

        var calcLength = this.node.position.sub(starPosition).mag();
        cc.log("进入到star——starPosition__【",starPosition,'】');
       /*  // cc.log("进入到star——calcLength__【",calcLength,'】');*/
        return calcLength;
    }

    private createStar() {
        // this.game.spawnNewStar();
        var game:any = new mainGame();
        game.randomStarFun();
        cc.log("进入到star——createStar__【",game.randomStarFun(),'】');

        //compare ? mainGame : game
        //星星的消失创建过程; 
        //this.node.parent.getComponent('gameScene').compare.randomStarFun();this.game ===> this.getComponent('gameScene');
        // this.node.parent.getComponent('mainGame').randomStarFun();
        //this.node.parent.getComponent()



        // this.node.destroy();
    }

    onLoad() {
        this.andMonsterInstance();
        /* // let xxx:any = cc.find('Canvas/PurpleMonster').getPosition();
        // cc.log("this123123",xxx);
        // cc.log("213",this);
        // cc.log("213232",this.game);
    }

    start() {
        // cc.log("[[star]]",this.node)
        /* // this.createStar(); */
        /* // cc.log("andMM___",this.andMonsterInstance()); */
    }

    private updateScore() {

    }
    destory(){
    }



    update (dt) {

        if(this.andMonsterInstance() < this.starSpace) {
             return this.createStar();
         }
    }
}
